package com.tanhua.manager.listener;

import com.tanhua.commons.template.BaiduCensorTemplate;
import com.tanhua.domain.mongo.Publish;
import com.tanhua.dubbo.api.PublishApi;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
@RocketMQMessageListener(topic = "tanhua-publish", consumerGroup = "tanhua-publish-consumer")
public class PublishMessageListener implements RocketMQListener<String> {

    @Autowired
    private BaiduCensorTemplate censorTemplate;
    @Reference
    private PublishApi publishApi;
    @Override
    public void onMessage(String publishId) {
        log.info("[动态审核]收到消息,topic:{},message:{}","tanhua-publish",publishId);
        Publish publish = publishApi.findById(publishId);
        if (publish.getState() == 0) {
            boolean textCensor = censorTemplate.textCensor(publish.getTextContent());
            boolean imageBatchCensor = censorTemplate.imageBatchCensor(publish.getMedias());
            publish.setState(textCensor && imageBatchCensor ? 1 : 2);
            publishApi.updateState(publish);
        }
    }
}
